package cn.lizhangqu.kb.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.api.LgrgApi;
import cn.lizhangqu.kb.model.Report;

/**
 * Created by kzc on 2015/11/8.
 */
public class ReportDetailActivity extends Activity{

    private WebView webView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        webView = (WebView) findViewById(R.id.webView);
//        textView = (TextView) findViewById(R.id. textView);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setUseWideViewPort(false);
        settings.setLoadsImagesAutomatically(true);
        settings.setBlockNetworkImage(false);

        Report report = (Report) getIntent().getSerializableExtra("report");
//        String content = report.getContent();
//        Document doc = Jsoup.parse(content);
//        content = doc.text();
//        Log.d("xxxx", content);
//        content = content.replace("   ","<br>   ");
//        String[] datas = content.split("   ");
//        Log.d("xxxx", datas.length+"");
//        Log.d("xxxx", datas[2]);
//        StringBuilder dataContent = null;
//        for (int i = 0 ; i < datas.length -1; i ++) {
//            dataContent.append(datas[i]);
//            dataContent.append("<br>");
//        }
//        content = dataContent.toString();
//        content = content.replaceAll("img src=\"", "img scr=\"" + LgrgApi.BASE_URL);

//        try {
//            InputStream in = getAssets().open("1.html");
//
//            byte[] data = new byte[in.available()];
//
//            in.read(data);
//
//            String temp = new String(data);
//
//            content = temp.replace("#{content}", content);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        String temp1 = null;
        try {
            InputStream in = getAssets().open("kzc.js");

            byte[] data = new byte[in.available()];

            in.read(data);

            temp1 = new String(data);


        } catch (IOException e) {
            e.printStackTrace();
        }



//        Log.d("xxxx", content);
//        webView.loadData(report.getContent(), "text/html", "UTF-8");
//        webView.loadDataWithBaseURL(null,content, "text/html", "UTF-8", null);

        //webView.loadData(content, "text/html", "UTF-8");

//        webView.loadUrl("http://www.lgrg-xm.com/articleDetail/20151029100000000263");
//        textView.setText(Html.fromHtml(content));
//        textView.setText(content);
        final String finalTemp = temp1;

//        webView.loadUrl("javascript:showDialog('zzzzz')");
        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.d("xxxx", "<==========>");
                webView.loadUrl("javascript:" + finalTemp);

            }


            @Override
            public void onPageFinished(WebView view, String url) {
                Log.d("xxxx", "<==========>!");
                webView.loadUrl("javascript:hint()");

//                webView.loadUrl("javascript:var imgs = document.getElementsByTagName('img');for(var i = 0; i<imgs.length; i++){imgs[i].style.width = '100%';imgs[i].style.height = 'auto';}");
//                super.onPageFinished(view, url);
            }
        });
        webView.loadUrl(LgrgApi.BASE_URL + LgrgApi.DETAIL + report.getId());

    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
////        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
////            webView.goBack();
////            return true;
////        }
//        return false;
//    }
}
